package com.tiger.project.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.tiger.common.core.domain.entity.SysUser;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.service.IPeopleInfoService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author bai
 * Date on 2023/2/3  15:14
 * Description:
 */
@Service
public class PeoPleInfoServiceImpl implements IPeopleInfoService {


    /**
     * 修改人员信息 ---OA
     * @param user 人员信息
     * @return
     */
    @Override
    public String updatePeople(SysUser user) throws IOException {
        String url = "http://www.ntgrzc.com/seeyon/rest/orgMember?token="+user.getToken();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orgAccountId", "670869647114347"); //单位ID
        map.put("orgDepartmentId", "-7168060802005712029");//部门ID
        map.put("orgLevelId", "-7612887728614715717");//职务ID
        map.put("orgPostId", "5524157176459250142");//岗位ID
        map.put("name", user.getNickName());//姓名
        map.put("telNumber", user.getPhonenumber());//移动电话
        map.put("emailAddress", user.getEmail());//电子邮件
        map.put("id",user.getOaId());
        if (StringUtils.isNotEmpty(user.getSex())){
            map.put("gender", user.getSex().equals("0") ? "1" : "2");//性别
        }
        map.put("loginName", user.getUserName());//登录名

        Response response = null;
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(map));
        Request request = new Request.Builder().url(url).method("PUT", body)
                .addHeader("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)").
                addHeader("Content-Type", "application/json").build();
        try {
            response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response.body().string();
    }


    /**
     * 人员停用----OA
     * @param oaId
     * @return
     */
    @Override
    public String deadPeople(String oaId,String token) throws IOException {
//        String url = "http://www.wxt.plus:22801/seeyon/rest/orgMember?token="+token;
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("id", oaId);//登录名
//        map.put("enabled","false");
//        System.out.println(map);
//        Response response = null;
//        OkHttpClient client = new OkHttpClient().newBuilder().build();
//        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
//        RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(map));
//        Request request = new Request.Builder().url(url).method("PUT", body)
//                .addHeader("User-Agent", "Apifox/1.0.0 (https://www.apifox.cn)").
//                addHeader("Content-Type", "application/json").build();
//        try {
//            response = client.newCall(request).execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return response.body().string();
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try{
            // 创建客户端连接对象
            httpClient = HttpClients.createDefault();
            String url = "http://www.ntgrzc.com/seeyon/rest/orgMember/"+oaId+"/enabled/false?token="+token;
            HttpPut put = new HttpPut(url);
            // 获取返回对象
            response = httpClient.execute(put);

            // 整理返回值
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity,"utf-8");
        }catch (Exception  e){
            e.printStackTrace();
        }finally {
            try{
                httpClient.close();
                response.close();
            }catch (IOException i){
                i.printStackTrace();
            }

        }

        return result;
    }
}
